package com.example.service.impl;

import com.example.mapper.*;
import com.example.pojo.Apartment;
import com.example.pojo.Indent;
import com.example.pojo.Permissions;
import com.example.pojo.User;
import com.example.service.IndentService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
public class IndentServiceImpl implements IndentService {

    @Autowired
    private IndentMapper indentMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Autowired
    private ContractpayMapper contractpayMapper;

    @Override
    public void addOrder(Indent indent, HttpServletRequest request) {
        Integer id = indent.getIdA();
        Apartment apartment = apartmentMapper.getApartmentId(id);
        User user1 = userMapper.getPim(apartment.getIdU());
        User user2 = userMapper.getPim(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        Permissions permissions = permissionsMapper.getidCard(apartment.getIdU());
        Permissions permissions1 = permissionsMapper.getidCard(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        indent.setName(apartment.getName());
        indent.setArea(apartment.getArea());
        indent.setPrice(apartment.getPrice());
        indent.setImage(apartment.getImage());
        String mm="#";
        for (int i = 0; i < 7; i++) {
            Random random=new Random();
             mm+=random.nextInt(10);
        }
        indent.setMm(mm);
        LocalDateTime now = LocalDateTime.now();
        indent.setSDate(now);
        indent.setZDate(now.plusMonths(indent.getY()));
        indent.setZfprice(apartment.getPrice()*indent.getY());
        indent.setZnickname(user2.getNickname());
        indent.setFnickname(user1.getNickname());
        indent.setTname(permissions1.getRealName());
        indent.setFname(permissions.getRealName());
        indent.setIdU(JwtUtils.parseJWT(request.getHeader("token")).get("id",Integer.class));
        indent.setFimage(user1.getImage());
        indent.setTimage(user2.getImage());
        indent.setTnumber(user2.getNumber());
        indent.setFnumber(user1.getNumber());
        indent.setFidCard(permissions.getIdCard());
        indent.setZidCard(permissions1.getIdCard());
        indent.setIdF(apartment.getIdU());
        indent.setRoomType(apartment.getRoomType());
        indent.setTimage(user2.getImage());
        indentMapper.addOrder(indent);
        contractpayMapper.addOrder(indent);
        apartmentMapper.setApartmentCu(id);
    }

    @Override
    public List<Indent> getOrder(String name, LocalDateTime date, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        List<Indent> list=indentMapper.getOrder(name,date,idU);
        return list;
    }

    @Override
    public void delOrder(Integer id, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        indentMapper.delOrder(id,idU);
    }


}
